import formInfo from '@ohos.app.form.formInfo';
import formBindingData from '@ohos.app.form.formBindingData';
import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
import Want from '@ohos.app.ability.Want';
import http from '@ohos.net.http';

export default class EntryFormAbility extends FormExtensionAbility {
  cityname : string='未获取到';
  citytianqi:string ='未获取到';
  cityqiwen:string ='未获取到';

  get_weather(){
    //1.创建Request请求任务
    let httpRequest = http.createHttp();
    //2.发起请求
    httpRequest.request(
      "http://api.yytianqi.com/observe",
      {
        method:http.RequestMethod.GET,
        header:{
          "Content-Type":"application/json",
          "charset":"utf-8"
        },
        extraData:{
          "city":"CH020100",   //上海
          "key":"7uvtcruc3escp027", //自己的key
        },
        connectTimeout:60000,
        readTimeout:60000
      },
      (err,data)=>{
        var weatherString = JSON.parse(data.result.toString());
        this.cityname = weatherString.data.cityName;
        this.citytianqi = weatherString.data.tq;
        this.cityqiwen = weatherString.data.qw;
      }
    )
  }

  onAddForm(want:Want) {
    // Called to return a FormBindingData object.
    this.get_weather();
    let formData = {
      "cityname":this.cityname,
      "citytianqi":this.citytianqi,
      "cityqiwen":this.cityqiwen
    };
    return formBindingData.createFormBindingData(formData);
  }

  onCastToNormalForm(formId) {
    // Called when the form provider is notified that a temporary form is successfully
    // converted to a normal form.
  }

  onUpdateForm(formId) {
    // Called to notify the form provider to update a specified form.
  }

  onChangeFormVisibility(newStatus) {
    // Called when the form provider receives form events from the system.
  }

  onFormEvent(formId, message) {
    // Called when a specified message event defined by the form provider is triggered.
  }

  onRemoveForm(formId) {
    // Called to notify the form provider that a specified form has been destroyed.
  }

  onAcquireFormState(want) {
    // Called to return a {@link FormState} object.
    return formInfo.FormState.READY;
  }
};